package org.krishna.mail;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;

@Service("sendMail")
public class SendMail {
	
		
	@Autowired
    private JavaMailSender mailSender;
	
	public void sendMailWithAttachment(){
		// reads form input
        final String emailTo = "";//request.getParameter("mailTo");
        final String subject = "abc";//request.getParameter("subject");
        final String messageSend = "myabc";//request.getParameter("message");
        final FileSystemResource file = new FileSystemResource("E:\\ab\\my.xml");
 
        // for logging
        System.out.println("emailTo: " + emailTo);
        System.out.println("subject: " + subject);
        System.out.println("message: " + messageSend);
        System.out.println("attachFile: " + file.getFilename());
        
    
        mailSender.send(new MimeMessagePreparator() {
 
            @Override
            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper messageHelper = new MimeMessageHelper(
                        mimeMessage, true, "UTF-8");
                
                messageHelper.setTo(emailTo);
                messageHelper.setSubject(subject);
                messageHelper.setText(messageSend);
                messageHelper.addAttachment(file.getFilename(), file);
                                
            }
 
        });
        /*MimeMessage message = mailSender.createMimeMessage();
        try{
    		MimeMessageHelper helper = new MimeMessageHelper(message, true);
     
    		helper.setFrom("krishchaitanya32@gmail.com");
    		helper.setTo(emailTo);
    		helper.setSubject(subject);
    		helper.setText(messageSend);
     
    		//FileSystemResource file = new FileSystemResource("C:\\log.txt");
    		helper.addAttachment(file.getFilename(), file);
     
    	     }catch (MessagingException e) {
    		throw new MailParseException(e);
    	     }
    	     mailSender.send(message);*/
		
	}
	
	public void sendMailWithAttachment(final String emailTo, String filePath, String parentName, String studentName){
		// reads form input
        //final String emailTo = "chaitu.msit@gmail.com";//request.getParameter("mailTo");
        final String subject = "Report Card of Student "+studentName.replaceAll(".xls", "");//request.getParameter("subject");
        final String messageSend = "Hi "+parentName;//request.getParameter("message");
        final FileSystemResource file = new FileSystemResource(filePath);
 
        // for logging
        System.out.println("emailTo: " + emailTo);
        System.out.println("subject: " + subject);
        System.out.println("message: " + messageSend);
        System.out.println("attachFile: " + file.getFilename());
        
    
        mailSender.send(new MimeMessagePreparator() {
 
            @Override
            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper messageHelper = new MimeMessageHelper(
                        mimeMessage, true, "UTF-8");
                
                messageHelper.setTo(emailTo);
                messageHelper.setSubject(subject);
                messageHelper.setText(messageSend);
                messageHelper.addAttachment(file.getFilename(), file);
                                
            }
 
        });
	}

}
